package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import pojo.Order;
import util.DBUtil;
import dao.OrderDao;

public class OrderDaoImpl implements OrderDao {

	@Override
	public int add(Order o) {
		String sql = "insert into orders(user_id, book_id, book_count, order_time) values(?, ?, ?, ?)";
		Connection conn = null;
		PreparedStatement pst = null;
		int count = 0;
		
		try {
			conn = DBUtil.createConnection();
			pst = conn.prepareStatement(sql);
			pst.setString(1, o.getUserId());
			pst.setString(2, o.getBookId());
			pst.setInt(3, o.getBookCount());
			pst.setDate(4, new java.sql.Date(o.getOrderTime().getTime()));
			
			count = pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(conn, pst, null);
		}
		return count;
	}

}
